package com.sanery.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sanery.dao.BankDao;
import com.sanery.entity.Record;
import com.sanery.service.BankService;

@Service("bankService")
public class BankServiceImpl implements BankService {
	@Autowired
	private BankDao dao = null;

	@Override
	public List<Record> getRecords(Integer id) {
		List<Record> list = dao.getRecords(id);
		return list;
	}

	@Override
	public String getCardNo(Integer id) {
		String cardno = dao.getCardNo(id);
		return cardno;
	}

	@Override
	public void disposit(Float money, String cardno, Integer id) {
		dao.addMoney(id, cardno, money);
		int type = 1;
		int cardId = dao.getCardId(cardno);
		dao.addRecord(cardId, money, type);
	}

	@Override
	public String getToAccount(String toAccount) {
		String name = dao.getToAccount(toAccount);
		if (name == null) {
			return "";
		}
		return name;
	}

	@Override
	public void transfer(Integer userId, String toAccount, Float money, String cardno) {
		int tocardId = dao.getCardId(toAccount);
		dao.addRecord(tocardId, money, 1);
		dao.updateMoney(money, toAccount);
		int fromcardId = dao.getCardId(cardno);
		dao.updateMoney(-money, cardno);
		dao.addRecord(fromcardId, money, 0);
	}

	@Override
	public Float getMoney(Integer id) {
		Float money = dao.getMoney(id);
		return money;
	}

}
